package net.lahuobao.mall.controller.admin;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.lahuobao.enums.mall.admin.EMallSubsidyType;
import net.lahuobao.mall.entity.admin.MallSubsidyDetailEntity;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import net.lahuobao.mall.entity.admin.MallSubsidyEntity;
import net.lahuobao.mall.service.admin.MallSubsidyServiceI;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.jeecgframework.core.beanvalidator.BeanValidators;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.net.URI;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;

/**   
 * @Title: Controller
 * @Description: 折扣
 * @author zhangdaihao
 * @date 2016-09-25 21:02:25
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/mallSubsidyController")
public class MallSubsidyController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MallSubsidyController.class);

	@Autowired
	private MallSubsidyServiceI mallSubsidyService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;
	

	/**
	 * 添加折扣
	 * 
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(MallSubsidyEntity mallSubsidy,MallSubsidyDetailEntity mallSubsidyDetailEntity, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		List<MallSubsidyDetailEntity> lists  = this.setParam(mallSubsidy, mallSubsidyDetailEntity, request);
		if (StringUtil.isNotEmpty(mallSubsidy.getId())) {
			message = "折扣更新成功";
			MallSubsidyEntity t = mallSubsidyService.get(MallSubsidyEntity.class, mallSubsidy.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(mallSubsidy, t);
				mallSubsidyService.update(t, lists);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "折扣更新失败";
			}
		} else {
			message = "折扣添加成功";
			mallSubsidyService.save(mallSubsidy, lists);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 设置参数
	 * @param mallSubsidy
	 * @param mallSubsidyDetailEntity
	 * @param request
	 * @return
	 */
	private List<MallSubsidyDetailEntity> setParam(MallSubsidyEntity mallSubsidy, MallSubsidyDetailEntity mallSubsidyDetailEntity,
												   HttpServletRequest request){
		List<MallSubsidyDetailEntity> paramList = new ArrayList<MallSubsidyDetailEntity>();
		if(mallSubsidy.getSubsidytype() != EMallSubsidyType.Mj.getValue()){//不是满减
			paramList.add(mallSubsidyDetailEntity);
		}else{//满减
			Integer index = Integer.valueOf(request.getParameter("index"));
			for(int i = 0; i < index; i++){
				MallSubsidyDetailEntity param = new MallSubsidyDetailEntity();
				param.setMinmoney(new BigDecimal(request.getParameter("minMoney_"+i)).setScale(2, BigDecimal.ROUND_HALF_UP));
				param.setMaxmoney(new BigDecimal(request.getParameter("maxMoney_" + i)).setScale(2, BigDecimal.ROUND_HALF_UP));
				param.setSubsidyval(new BigDecimal(request.getParameter("subsidyVal_"+i)).setScale(2, BigDecimal.ROUND_HALF_UP));
				paramList.add(param);
			}
		}
		return paramList;
	}

}
